package com.happle.DAL;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.happle.gcmclient.config.Constants;

public class DBHelper extends SQLiteOpenHelper {

    // table conversations
	static String SQL_CREATE_TABLE_CONVERSATION = "CREATE TABLE "
			+ Constants.TBL_CONVERSATIONS + "( "
			+ "msg_id_local STRING PRIMARY KEY, "
			+ "wave_id STRING NOT NULL, " 
			+ "is_owner INTEGER,"
			+ "message TEXT," 
			+ "lng_id NUMERIC," 
			+ "datetime TEXT,"
			+ "msg_status NUMERIC," 
			+ "msg_order INTEGER,"
			+ "is_sent INTEGER,"
            + "is_new INTEGER);";

    // table languages
    static String SQL_CREATE_TABLE_LANGUAGE = "CREATE TABLE "
            + Constants.TBL_LANGUAGES + "( "
            + "lng_id STRING PRIMARY KEY, "
            + "lng_name STRING, "
            + "is_marked INTEGER,"
            + "is_sent INTEGER);";

    // table conversations not sync
    static String SQL_CREATE_TABLE_CONVERSATIONS_NOT_SYNC = "CREATE TABLE "
            + Constants.TBL_CONVERSATIONS_NOT_SYNC + " ( "
            + "wave_id STRING NOT NULL, "
            + "lng_id NUMERIC,"
            + "is_owner INTEGER,"
            + "conv_status NUMERIC)";

	// Constructor, defines the DB name & version
	public DBHelper(Context context) {
		super(context, Constants.DATABASE_NAME, null,
				Constants.DATABASE_VERSION);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_TABLE_CONVERSATION);
        db.execSQL(SQL_CREATE_TABLE_LANGUAGE);
        db.execSQL(SQL_CREATE_TABLE_CONVERSATIONS_NOT_SYNC);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + Constants.TBL_CONVERSATIONS);
        db.execSQL("DROP TABLE IF EXISTS " + Constants.TBL_LANGUAGES);
        db.execSQL("DROP TABLE IF EXISTS " + Constants.TBL_CONVERSATIONS_NOT_SYNC);
		onCreate(db);
	}
}
